package cn.tedu.sp11.fallback;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;

import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;


@Slf4j
@Component
public class OrderServiceFallback implements FallbackProvider {
	/**
	 * 返回一个服务ID,来确定哪一个服务失败时来执行该降级类
	 * 返*和null都表示所有微服务失败都应用当前降级类
	 */
	@Override
	public String getRoute() {
		//当执行item-service失败,执行该降级类
		return "item-service";
		//返*和null都表示所有微服务失败都应用当前降级类
	}

	@Override
	public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
		return response();//调用自定方法response()
	}

	private ClientHttpResponse response() {
		return new ClientHttpResponse() {
			//下面三个方法都是返回三种类型的响应状态码
            @Override
            public HttpStatus getStatusCode() throws IOException {
                return HttpStatus.OK;
            }
            @Override
            public int getRawStatusCode() throws IOException {
                return HttpStatus.OK.value();
            }
            @Override
            public String getStatusText() throws IOException {
                return HttpStatus.OK.getReasonPhrase();
            }

            @Override
            public void close() {
            }
            /**
             * 降级类的逻辑主体,也是降级方法逻辑主体	
             */
            @Override
            public InputStream getBody() throws IOException {
            	log.info("fallback body");
            	String s = JsonResult.err().msg("后台服务错误").toString();
                return new ByteArrayInputStream(s.getBytes("UTF-8"));
            }

            @Override
            public HttpHeaders getHeaders() {
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.APPLICATION_JSON);
                return headers;
            }
           
        };
	}

}
